<?php
class ujn_model {

	protected $db_name;
	protected $tbl_name;

	/**
	 * @var tls_mysql
	 */
	protected $_db;

	/**
	 *
	 * @return tls_mysql
	 */
	function getDB() {
		return $this->_db;
	}

	function getTblName() {
		return $this->tbl_name;
	}

	// =============== MySQL =================
	function mysql($alias = '') {

		$alias = ( '' == $alias ) ? ujn_runtime::getSplitAlias() : $alias;

		$mysql_source = ujn_runtime::getParam('mysql_source');
		if (isset($mysql_source[$alias])) {
			if(ujn_runtime::getParam('mysql_seldb') != $this->db_name) {
				ujn_runtime::setParam('mysql_seldb', $this->db_name);
				mysql_select_db($this->db_name);
			}
			return $mysql_source[$alias];
		}

		$_servers = array();

		$dhs = ujn::getCFG("dhs.{$alias}.mysql");
		if(false != $dhs) {
			list($account, $host) = explode('@', $dhs);

			list($user, $pswd) = explode(':', $account);

			$obj = new tls_mysql($host, $user, $pswd, $this->db_name);

// @TODO: 不加这个 在6上会中文乱码 囧啊
$_sql = "set names 'utf8';";
$obj->query($_sql);

			ujn_runtime::appendParam('mysql_source', $alias, $obj);
			return $obj;
		} else
			die('error get mysql cfg');
	}

	function getLastSql() {
		return $this->_db->getLastSql();
	}

	// =============== Tyrant =================
	/**
	 *
	 * @return tls_tyrant
	 */
	function tt() {

		$alias = ujn_runtime::getSplitAlias();

		$tyrant_source = ujn_runtime::getParam('tyrant_source');
		if (isset($tyrant_source[$alias])) return $tyrant_source[$alias];

		$dhs = ujn::getCFG("dhs.{$alias}.TokyoTyrant");
		if(false != $dhs) {
			$obj = new tls_tyrant($dhs, false);
			ujn_runtime::appendParam('tyrant_source', $alias, $obj);
			return $obj;
		} else
			return false;
	}

	// =============== MemCache =================
	/**
	 *
	 * @return tls_memcache
	 */
	function mc($flag = '', $alias = '') {

		$flag  = ( '' != $flag )	? "_{$flag}"	: '';
		$alias = ( '' != $alias )	? $alias		: ujn_runtime::getSplitAlias();

		$memcache_source = ujn_runtime::getParam('memcache_source');
		if (isset($memcache_source["{$alias}_{$flag}"])) return $memcache_source["{$alias}_{$flag}"];

		$dhs = ujn::getCFG("dhs.{$alias}.memcache{$flag}");
		if(false != $dhs) {
			$_servers = array();

			if(is_array($dhs)) { // 兼容旧版
				foreach($dhs as $k => $weight) {
					list($host, $port) = explode(':', $k);
					$_servers[] = array( $host, $port, $weight );
				}
			} else {
				list($host, $port) = explode(':', $dhs);
				$_servers[] = array( $host, $port, '100' );
			}
			$obj = new tls_memcache(array($_servers));
			ujn_runtime::appendParam('memcache_source', "{$alias}_{$flag}", $obj);
			return $obj;
		} else
			return false;
	}

	// =============== HTTPSQS =================
	/**
	 *
	 * @return tls_httpsqs
	 */
	function httpsqs() {
		return tls_httpsqs::getInstance();
	}

	// =============== Redis =================
	/**
	 *
	 * @return tls_redis
	 */
	function redis() {

		$alias = ujn_runtime::getSplitAlias();

		$_source = ujn_runtime::getParam('redis_source');
		if (isset($_source[$alias])) return $_source[$alias];

		$dhs = ujn::getCFG("dhs.{$alias}.redis");
		if(false != $dhs) {
			$obj = new tls_redis($dhs);
			ujn_runtime::appendParam('redis_source', $alias, $obj);
			return $obj;
		} else
			return false;
	}
	function redis_power($flag = '', $alias = '') {

		$flag  = ( '' != $flag )	? "_{$flag}"	: '';
		$alias = ( '' != $alias )	? $alias		: ujn_runtime::getSplitAlias();

		$_source = ujn_runtime::getParam('redis_source');
		if (isset($_source["{$alias}_{$flag}"])) return $_source["{$alias}_{$flag}"];

		$dhs = ujn::getCFG("dhs.{$alias}.redis{$flag}");
		if(false != $dhs) {
			$obj = new tls_redis($dhs);
			ujn_runtime::appendParam('redis_source', "{$alias}_{$flag}", $obj);
			return $obj;
		} else
			return false;
	}

	// =============== MongoDB =================


    protected static $_instance = array();

    /**
     * 单例模型
     * @return ujn_model
     */
	static function getInstance($model_name) {
        if(!isset(ujn_model::$_instance[$model_name])) {
            ujn_model::$_instance[$model_name] = new $model_name($model_name);
        }
        return ujn_model::$_instance[$model_name];
	}

	protected function __construct($tbl_name) {
//		$model_name = get_class($this);
//		ujn_model::$_instance[$model_name] = $this;
		$this->db_name		= ujn::getCFG('database_name');
		$this->tbl_name		= $tbl_name;
	}

	protected function uint($num){
		return tls_func::uint($num);
	}

	protected function escape($string){
		return tls_func::escape($string);
	}
}